var express = require('express');
var router = express.Router();
var pool = require('../config/mysqlconfig')
// 获取数据
// http://localhost:3000/admin/all
router.get('/all',function(req,res){
  pool.getConnection(function(err,conn){
    if(err){
      console.log(err)
    }else{
      let sql = "select * from admin"
      conn.query(sql,function(error,results){
        if(error){
          console.log("数据库连接失败")
        }
        res.send(results)
        conn.release()
      })
    }
  })
})

// 添加数据
// http://localhost:3000/admin/add

router.post('/add',function(req,res){
  pool.getConnection(function(err,conn){
      if(err){
          console.log(err)
      }else{
          
        let ausername = req.body.username;
        let apassword =  req.body.password;
        let aname = req.body.name;
        let arole = req.body.role;
        let acreate = new Date();
        let aupdate = new Date();
        let alastloginat = new Date();
          let data={
            username:ausername,
            password:apassword,
            name:aname,
            role:arole,
            created_at:new Date(acreate),
            updated_at:aupdate,
            last_login_at:alastloginat
          }
          let sql ="insert into admin set ?"
          conn.query(sql,data,function(error,results){
              if(error){
                  console.log(error)
              }
              res.send(results)
              conn.release()
          })
      }
  })
})
// 删除数据
// http://localhost:3000/article/del

router.delete('/del',function(req,res){
  pool.getConnection(function(err,conn){
    if(err){
      console.log(err)
    }else{
      let id = req.body.id;
      let sql = " delete from admin where id=?"
      conn.query(sql,id,function(error,results){
        if(error){
          console.log(error)
        }
        res.send(results)
        conn.release()
      })
    }
  })
})
// 更新数据
// http://localhost:3000/admin/del
router.put('/update',function(req,res){
  pool.getConnection(function(err,conn){
    if(err){
      console.log(err)
    }else{
      let username = req.body.username;
      
      let name = req.body.name;
      let role = req.body.role;
      // let create = req.body.created_at;
      let update = new Date();
      let lastloginat = new Date();
      let id = req.body.id;
      let data=[username,name,role,update,lastloginat,id]
      let sql = "update admin set username=?,name=?,role=?,updated_at=?,last_login_at=? where id=?"

      conn.query(sql,data,function(error,results){
        if(error){
          console.log(error)
        }
        res.send(results)
        conn.release()
      })
    }
  })
})

// 查找数据
// http://localhost:3000/admin/select
router.post('/sel',function(req,res){
  pool.getConnection(function(err,conn){
      if(err){
          console.log(err)
      }else{
          let id = req.body.id
          let sql = "select * from admin where id=?"
          conn.query(sql,id,function(error,results){
              if(error){
                  console.log(error)
              }
              res.send(results)
              conn.release()
          })
      }
  })
})
module.exports = router;
